package com.mach.platform.config;

import com.google.code.kaptcha.Constants;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.util.Config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.Properties;

/**
 * @author Anonymous
 * @Description:
 * @Package: com.mach.platform.config
 * @Time create on 2017/11/18 15:40
 */
@Configuration
public class MvcConfig implements WebMvcConfigurer {

	@Bean
	public DefaultKaptcha defaultKaptcha ( ) {

		DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
		Properties properties = new Properties();

		properties.setProperty(Constants.KAPTCHA_BORDER, "yes");
		properties.setProperty(Constants.KAPTCHA_BORDER_COLOR, "105,179,90");
		properties.setProperty(Constants.KAPTCHA_TEXTPRODUCER_FONT_COLOR, "black");
		properties.setProperty(Constants.KAPTCHA_TEXTPRODUCER_FONT_SIZE, "22");
		properties.setProperty(Constants.KAPTCHA_IMAGE_WIDTH, "125");
		properties.setProperty(Constants.KAPTCHA_IMAGE_HEIGHT, "30");
		properties.setProperty(Constants.KAPTCHA_TEXTPRODUCER_CHAR_STRING, "012345689");
		properties.setProperty(Constants.KAPTCHA_TEXTPRODUCER_CHAR_LENGTH, "4");
		//这里的字体需要 在 所运行的系统中存在，否则，验证码会显示乱码。如果linux没有该字体，需要上传到linux去才可显示
		properties.setProperty(Constants.KAPTCHA_TEXTPRODUCER_FONT_NAMES, "Arial");
		properties.setProperty(Constants.KAPTCHA_NOISE_IMPL, "com.google.code.kaptcha.impl.NoNoise");
		properties.setProperty(Constants.KAPTCHA_OBSCURIFICATOR_IMPL, "com.google.code.kaptcha.impl.ShadowGimpy");
		Config config = new Config(properties);
		defaultKaptcha.setConfig(config);
		return defaultKaptcha;
	}

	/**
	 * 允许跨域访问
	 * @param registry
	 */
//	@Override
//	public void addCorsMappings (CorsRegistry registry) {
//		registry.addMapping("/**").allowCredentials(true).allowedHeaders("*").allowedOrigins("*").allowedMethods("*");
//	}


}
